﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibary
{
    public  class WebClientSingletonHelper
    {

        public  IHttpClientFactory _httpClientFactory;

        public WebClientSingletonHelper(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
            
        }

        public  async Task<String> ClientGetAsync(string url, string token)
        {
            using (var _httpClient = _httpClientFactory.CreateClient())
            {
                _httpClient.Timeout = new TimeSpan(0, 0, 5);
                using (var hc = new HttpRequestMessage())
                {
                    hc.RequestUri = new Uri(url);
                    if (!string.IsNullOrEmpty(token))
                        hc.Headers.Add("Authorization", token);
                    hc.Method = new HttpMethod("GET");

                    using (var rps = await _httpClient.SendAsync(hc))
                    {
                        if (rps.IsSuccessStatusCode)
                            return await rps.Content.ReadAsStringAsync();
                        else
                            return "";
                    }
                }
            }
        }

        public  async Task<T> ClientGetAsync<T>(string url, string token)
        {
            string t = await ClientGetAsync(url, token);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(t);
        }

        public  async Task<String> ClientPostAsync(string url, string data, string token)
        {
            using (var _httpClient = _httpClientFactory.CreateClient())
            {
                //_httpClient.Timeout = new TimeSpan(0, 0, 5);
                using (var hc = new HttpRequestMessage())
                {
                    hc.RequestUri = new Uri(url);
                    if (!string.IsNullOrEmpty(token))
                        hc.Headers.Add("Authorization", token);
                    hc.Method = new HttpMethod("POST");
                    hc.Content = new StringContent(data);
                    hc.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    using (var rp = await _httpClient.SendAsync(hc))
                    {
                        if (rp.IsSuccessStatusCode)
                            return await rp.Content.ReadAsStringAsync();
                        else
                            return "";
                    }
                }
            }
        }

        public  async Task<T> ClientPostAsync<T>(string url, string data, string token)
        {
            string t = await ClientPostAsync(url, data, token);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(t);
        }

        public  async Task<T> ClientPostAsync<T>(string url, Object data, string token)
        {
            string t = await ClientPostAsync(url, data, token);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(t);
        }

        public  async Task<String> ClientPostAsync(string url, Object data, string token)
        {
            return await ClientPostAsync(url, Newtonsoft.Json.JsonConvert.SerializeObject(data), token);
        }
    }
}
